/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogModificarUnidad.java
 *
 * Created on 07-mar-2010, 12:37:43
 */

package es.atareao.metron.gui;

import es.atareao.alejandria.gui.ErrorDialog;
import es.atareao.alejandria.lib.Convert;
import es.atareao.alejandria.val.gui.GuiNotEmptyValidator;
import es.atareao.metron.lib.Cuadro;
import java.awt.Component;
import java.sql.SQLException;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author atareao
 */
public class DialogCuadro extends javax.swing.JDialog implements Dialogo {
    //
    //******************************CONSTRUCTORES*******************************
    //

    /** Creates new form DialogModificarUnidad */
    public DialogCuadro(Cuadro cuadro,int operacion) {
        super(new JFrame(),true);
        initComponents();
        this.setSize(380, 200);
        this.setLocationRelativeTo(null);
        String variable="Cuadro";
        //
        switch (operacion){
            case OP_ADD:
                this.setTitle("Añadir "+variable);
                break;
            case OP_EDIT:
                this.setTitle("Editar "+variable);
                break;
            case OP_DELETE:
                this.setTitle("Borrar "+variable);
                this.doNotEditable();
                break;
            case OP_VIEW:
                this.setTitle("Consultar "+variable);
                this.doNotEditable();
                break;
        }
        this.setCuadro(cuadro);
    }
    //
    //**************************METODOS AUXILIARES******************************
    //
    private boolean valida(JComponent main){
        boolean ans=true;
        InputVerifier iv=main.getInputVerifier();
        if(iv!=null){
            if(!iv.verify(main)){
                ans=false;
            }
        }
        for(Component c:main.getComponents()){
            if(c instanceof JComponent){
                if(!valida(((JComponent)c))){
                    ans=false;
                }
            }
        }
        return ans;
    }
    private boolean valida(){
        return this.valida(this.jPanel1);
    }
    private void doClose(int retStatus) {
        if(retStatus==RET_OK){
            System.out.println("Registro nº: "+this.getCuadro().getId());
            if((Convert.toLong(this.getCuadro().getId())>0)&&(Convert.toLong(this.getCuadro().getId())<4)){
                JOptionPane.showMessageDialog(this,"Este registro no se puede modificar ni borrar.","ATENCIÓN",JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if(this.valida()){
                try {
                    Cuadro fd=this.fromDialog();
                    if((fd!=null)&&(fd.validate())){
                        this._cuadro=fd;
                    }else{
                        return;
                    }
                } catch (Exception ex) {
                     ErrorDialog.manejaError(ex,false);
                     return;
                }
            }
        }
        this.setReturnStatus(retStatus);
        setVisible(false);
        dispose();
    }

    private void doNotEditable(){
        this.jDescripcion.setEditable(false);
        this.jFecha.setEditable(false);
        //
        this.jDescripcion.setBackground(java.awt.SystemColor.info);
        this.jFecha.setBackground(java.awt.SystemColor.info);
    }

    private Cuadro fromDialog(){
        try {
            Cuadro cuadro = new Cuadro(this._cuadro.getConector());
            cuadro.setId(this._cuadro.getId());
            cuadro.setValue("descripcion", this.jDescripcion.getText());
            cuadro.setValue("fecha", Convert.toStringDateSql(this.jFecha.getDate()));
            return cuadro;
        } catch (SQLException ex) {
            ErrorDialog.manejaError(ex);
        }
        return null;
    }
    private void toDialog(){
        this.jDescripcion.setText(this._cuadro.getValue("descripcion"));
        this.jFecha.setDate(Convert.toDateSql(this._cuadro.getValue("fecha")));
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jDescripcion = new javax.swing.JTextField();
        jFecha = new es.atareao.alejandria.gui.JDateChooser();
        jAceptar = new javax.swing.JButton();
        jCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Descripción:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 90, 30));

        jLabel3.setText("Fecha:");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 90, 30));

        jDescripcion.setInputVerifier(new GuiNotEmptyValidator(this,jDescripcion));
        jPanel1.add(jDescripcion, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, 230, 30));
        jPanel1.add(jFecha, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 230, 30));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 12, 350, 90));

        jAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_ok.png"))); // NOI18N
        jAceptar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jAceptar.setBorderPainted(false);
        jAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(jAceptar, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 110, -1, -1));

        jCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_cancel.png"))); // NOI18N
        jCancelar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jCancelar.setBorderPainted(false);
        jCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(jCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 110, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAceptarActionPerformed
        this.doClose(Dialogo.RET_OK);
    }//GEN-LAST:event_jAceptarActionPerformed

    private void jCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelarActionPerformed
        this.doClose(Dialogo.RET_CANCEL);
    }//GEN-LAST:event_jCancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAceptar;
    private javax.swing.JButton jCancelar;
    private javax.swing.JTextField jDescripcion;
    private es.atareao.alejandria.gui.JDateChooser jFecha;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    private Cuadro _cuadro;
    private int _returnStatus = RET_CANCEL;
    //
    //**************************METODOS DE ACCESO*******************************
    //

    @Override
    public int getReturnStatus() {
        return _returnStatus;
    }

    @Override
    public void setReturnStatus(int returnStatus) {
        this._returnStatus = returnStatus;
    }

    /**
     * @return the _cuadro
     */
    public Cuadro getCuadro() {
        return _cuadro;
    }

    /**
     * @param cuadro the _cuadro to set
     */
    public void setCuadro(Cuadro cuadro) {
        this._cuadro = cuadro;
        this.toDialog();
    }
}
